package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl implements DishService {
    @Autowired (required = false)
    private DishMapper dishMapper;

    @Autowired(required = false)
    private DishFlavorMapper dishFlavorMapper;

    @Autowired(required = false)
    private CategoryMapper categoryMapper;

    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public void saveWithFlavor(DishDto dishDto) {
        dishDto.setCreateTime(LocalDateTime.now());
        dishDto.setUpdateTime(LocalDateTime.now());
        dishDto.setStatus(1);
        dishDto.setSort(0);
        dishMapper.save(dishDto);

        List<DishFlavor> flavors = dishDto.getFlavors();

        List<DishFlavor> dishFlavorList = flavors.stream().map((flavor -> {
            //对每一个元素进行加工
            flavor.setDishId(dishDto.getId());
            flavor.setCreateTime(LocalDateTime.now());
            flavor.setUpdateTime(LocalDateTime.now());
            flavor.setCreateUser(dishDto.getCreateUser());
            flavor.setUpdateUser(dishDto.getUpdateUser());
            return flavor;
        })).collect(Collectors.toList());

//        List<DishFlavor> flavorList = dishDto.getFlavors();
//        for (DishFlavor flavor : flavorList) {
//            flavor.setDishId(dishDto.getId());
//            flavor.setCreateTime(LocalDateTime.now());
//            flavor.setUpdateTime(LocalDateTime.now());
//            flavor.setCreateUser(dishDto.getCreateUser());
//            flavor.setUpdateUser(dishDto.getUpdateUser());
//        }

        dishFlavorMapper.saveBatch(dishFlavorList);
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
    }

    @Override
    public Page<DishDto> page(Integer page, Integer pageSize, String name) {
        PageHelper.startPage(page, pageSize);
        List<Dish> dishList = dishMapper.findByName(name);
        PageInfo<Dish> pageInfo = new PageInfo<>(dishList);

        List<DishDto> collect = pageInfo.getList().stream().map((dish -> {
            DishDto dishDto = new DishDto();
            //将dish中的数据传给dishdto
            BeanUtils.copyProperties(dish, dishDto);
            Category category = categoryMapper.findById(dish.getCategoryId());
            dishDto.setCategoryName(category.getName());
            return dishDto;
        })).collect(Collectors.toList());

        Page<DishDto> pageResult = new Page<>(collect,pageInfo.getTotal(),pageSize,page);

        return pageResult;
    }

    @Override
    public DishDto findById(Long id) {
        Dish dish = dishMapper.findById(id);

        List<DishFlavor> dishFlavorList = dishFlavorMapper.findByDishId(id);

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);
        dishDto.setFlavors(dishFlavorList);
        return dishDto;

    }

    @Override
    public void updateWithFlavor(DishDto dishDto) {
        dishDto.setUpdateTime(LocalDateTime.now());
        //修改菜品
        dishMapper.updateById(dishDto);

        //删除菜品的所有口味信息
        dishFlavorMapper.deleteFlavorList(dishDto.getId());

        //补全口味信息
        List<DishFlavor> flavors = dishDto.getFlavors();
        List<DishFlavor> dishFlavorList = flavors.stream().map((flavor -> {
            //对每一个元素进行加工
            flavor.setDishId(dishDto.getId());
            flavor.setCreateTime(LocalDateTime.now());
            flavor.setUpdateTime(LocalDateTime.now());
            flavor.setCreateUser(dishDto.getCreateUser());
            flavor.setUpdateUser(dishDto.getUpdateUser());
            return flavor;
        })).collect(Collectors.toList());

        dishFlavorMapper.saveBatch(dishFlavorList);

        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
    }

    @Override
    public List<DishDto> findByCategoryId(Long categoryId,Integer status) {
        List<DishDto> dishDtoList = null;
        String key = "dish_" + categoryId;

        dishDtoList = (List<DishDto>) redisTemplate.opsForValue().get(key);

        if (dishDtoList == null){
            List<Dish> dishList = dishMapper.findByCategoryId(categoryId, status);
            dishDtoList = dishList.stream().map(dish -> {
                DishDto dishDto = new DishDto();
                //属性赋值
                BeanUtils.copyProperties(dish, dishDto);

                //查看该菜品的口味信息
                List<DishFlavor> dishFlavorList = dishFlavorMapper.findByDishId(dish.getId());
                dishDto.setFlavors(dishFlavorList);

                //类别信息
                Category category = categoryMapper.findById(dish.getCategoryId());
                dishDto.setCategoryName(category.getName());
                return dishDto;
            }).collect(Collectors.toList());
            redisTemplate.opsForValue().set(key,dishDtoList);
        }
        return dishDtoList;
    }

    @Override
    public void deletByIds(List<Long> ids) {
        //删除菜品
        dishMapper.deleteByIds(ids);

        //删除菜品对应得口味
        for (int i = 0; i < ids.size(); i++) {
            dishFlavorMapper.deleteFlavorList(ids.get(i));
        }

    }

    @Override
    public void setStatus(Integer status, List<Long> ids) {
        dishMapper.setStatus(status,ids);
    }
}























